import json

from django.http import HttpResponse, JsonResponse

from History.models import History
from Message.models import Message
def addMessage(request):
    if request.method == "POST":
        body = json.loads(request.body)
        Message.objects.create(
            type=body["type"],
            senderAccount=body["senderAccount"],
            accepterAccount=body["accepterAccount"],
            content=body["content"]
        ).save()
        return HttpResponse("添加消息完成")
    else:
        return HttpResponse("请使用POST")
def queryMsg(request):
    if request.method == "GET":
        msg = Message.objects.filter(accepterAccount=request.GET.get("account"))
        return JsonResponse({"data": [{
            'type': each.type,
            'senderAccount': each.senderAccount,
            'accepterAccount': each.accepterAccount,
            'content': each.content,
            'createTime': each.createTime,
            'id': each.id,
        } for each in msg.order_by('-createTime')]
        })
    else:
        return HttpResponse("请使用GET")
def querySingle(request):
    sender = []
    accepter = []
    senders = History.objects.order_by("createTime").filter(accepterAccount=request.GET.get("account"))
    accepters = History.objects.order_by("createTime").filter(senderAccount=request.GET.get("account"))
    for each in senders:
        if not each.senderAccount in sender:
            sender.append(each.senderAccount)
    for each in accepters:
        if not each.accepterAccount in accepter:
            accepter.append(each.accepterAccount)
    merge = []
    mergeName = []
    for send in sender:
        for accept in accepter:
            if send == accept:
                s = senders.filter(accepterAccount=request.GET.get("account"), senderAccount=send).last()
                a = accepters.filter(senderAccount=request.GET.get("account"), accepterAccount=accept).last()
                if s.createTime > a.createTime:
                    merge.append(s)
                    mergeName.append(send)
                else:
                    merge.append(a)
                    mergeName.append(accept)
    # 没有互相发过消息的人
    for each in sender:
        if not each in mergeName:
            merge.append(senders.filter(accepterAccount=request.GET.get("account"), senderAccount=each).last())
    for each in accepter:
        if not each in mergeName:
            merge.append(accepters.filter(senderAccount=request.GET.get("account"), accepterAccount=each).last())
    return JsonResponse({
        "data": [{
            "type": each.type,
            "content_type": each.content_type,
            "senderAccount": each.senderAccount,
            "accepterAccount": each.accepterAccount,
            "content": each.content,
            "createTime": each.createTime
        } for each in merge]})
def deleteMsg(request):
    if request.method == "GET":
        Message.objects.filter(id=request.GET.get("id")).delete()
        return HttpResponse("删除成功")
    else:
        return HttpResponse("请使用GET")
